package com.hymjweb.demo.hymj.bill_code_manage.b_native.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.DateUtil;
import com.hymjweb.demo.hymj.bill_code_manage.b_native.bean.BillNativeCodePojo;
import com.hymjweb.demo.hymj.bill_code_manage.b_native.service.BillNativeCodeService;
import com.hymjweb.demo.hymj.department_manage.bean.XtDepartmentPojo;
import com.hymjweb.demo.hymj.system_maintenance.user.bean.XtUserPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.hymj.bill_code_manage.b_native.controller
 * @Author: luoge
 * @CreateTime: 2020-10-31 20:28
 * @Description:
 */

@Controller
@RequestMapping("/pages/bill_code_manage/b_native")
public class BillNativeCodeController extends BaseController {

    @Autowired
    private BillNativeCodeService billNativeCodeService;

    /**
     * 分单号创建初化
     *
     * @return
     */
    @RequestMapping("/createbillnativecode")
    public String createbillnativecode() {
        XtUserPojo xtUserPojo = this.getLoginUser();
        XtDepartmentPojo xtDepartmentPojo = this.getLoginUserDepartment();
        setRequestAttribute("departmentId", xtDepartmentPojo.getId());
        setRequestAttribute("departmentName", xtDepartmentPojo.getName());

        setRequestAttribute("userId", xtUserPojo.getId());
        setRequestAttribute("userName", xtUserPojo.getName());

        setRequestAttribute("currentDate", DateUtil.getSysDateStr());

        return "pages/bill_code_manage/b_native/createbillnativecode";
    }

    /**
     * 分单查询初始化
     * @return
     */
    @RequestMapping("/billNativeCode")
    public String billNativeCode() {
        setRequestAttribute("currentDate", DateUtil.getSysDateStr());
        return "pages/bill_code_manage/b_native/billNativeCode";
    }

    @RequestMapping("/queryBillNativeCodeList")
    @ResponseBody
    public Object queryBillNativeCodeList(@RequestParam(required = false) Map param) {
        return billNativeCodeService.queryBillNativeCodeList(param);
    }

    /**
     * 显示单号
     * @param userId
     * @return
     */
    @RequestMapping("/showCode")
    @ResponseBody
    public Object showCode(@RequestParam(value = "userId", required = true) String userId) {
        return billNativeCodeService.showCode(userId);
    }

    @RequestMapping("/checkBillCodeId")
    @ResponseBody
    public Object checkBillCodeId(@RequestBody Map<String, Object> param) throws Exception {

        if (param == null || param.isEmpty()) {
            throwException("检测分单号使用情况参数错误!");
        }

        String billCodeId = DataTypeUtil.toNotNullString(param.get("billCodeId"));

        if (billCodeId == null || DataTypeUtil.isEmptyStr(billCodeId)) {
            throwException("待检测分单号参数为空!");
        }

        boolean result = billNativeCodeService.checkBillCodeId(billCodeId);
        String msg = result ? "可以使用" : "已经被占用或作废";

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(result);
        baseResultMap.setMessage(msg);

        return baseResultMap;
    }

    /**
     * 生成分单号,没有分页功能
     * @param param
     * @return
     */
    @RequestMapping("/generateCode")
    @ResponseBody
    public Object generateCode(@RequestBody Map<String, Object> param) throws Exception {
        if (param == null || param.isEmpty()) {
            throwException("分单号生成参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("分单号生成成功!");

        boolean flag = billNativeCodeService.generateCode(param);
        if (!flag) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("分单号生成失败!");
        }

        return baseResultMap;
    }

    /**
     * 分单列表保存
     * @param billNativeCodePojoList
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveCode")
    @ResponseBody
    public Object saveCode(@RequestBody List<BillNativeCodePojo> billNativeCodePojoList) throws Exception {

        if (billNativeCodePojoList == null || billNativeCodePojoList.isEmpty()) {
            throwException("分单号保存参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("分单号保存成功!");

        boolean result = billNativeCodeService.saveCode(billNativeCodePojoList);
        if (!result) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("分单号保存失败,请重试!");
        }

        return baseResultMap;
    }


    @RequestMapping("/deleteNativeBillCode")
    @ResponseBody
    public Object deleteNativeBillCode(@RequestBody(required = true) Map param) throws Exception {
        if (param == null || param.isEmpty()) {
            throwException("分单号删除参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("分单号删除成功!");

        // {deleteNativeBillCodes:canDeleteCodes}
        int affectRows = billNativeCodeService.deleteNativeBillCode(param);

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("分单号删除失败!");
        }
        return baseResultMap;
    }

}
